﻿using AutoMapper;
using MediatR;
using ProductionLine.API.Application.Command.Car;
using ProductionLine.Domain.Car;
using ProductionLine.Domain.ProductLine;
using ProductionLine.ErrorCode;
using ProductionLine.Infrastructure.Implement;

namespace ProductionLine.API.Application.Handler.Car
{
    public class AddProductLineHandler : IRequestHandler<AddProductLineCommand, APIResult<int>>
    {
        IMapper mapper;
        DatabaseContext db;

        public AddProductLineHandler(IMapper mapper, DatabaseContext db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(AddProductLineCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res=new APIResult<int>();
            var model = mapper.Map<ProductLineModel>(request);
            int r = db.SqlSugar.Insertable(model).ExecuteCommand();
            res.Msg = "添加生产线成功";
            res.Data = r;
            res.Code = APIEnum.Success;
            return await Task.FromResult(res);
        }
    }
}
